const { BASE_URL } = getApp().globalData

class Request{
	constructor(arg) {
		if(arg && arg.constructor.name=="Object"){
			return this.setOption(arg)
		}
	}
	
	setOption({url,type="GET",data={}}){
		return new Promise((resolve,reject)=>{
			uni.request({
				url: BASE_URL + url,
				data: data,
				method:type,
				header: {
					"content-type":"application/x-www-form-urlencoded"
				},
				success({data}){
					if(data.code==1){
						resolve(data.data)
					}else{
						uni.showToast({
						    title: data.msg,
							icon:"none"
						})
					}
				},
				fail(err){
					uni.showToast({
					    title: err.errMsg,
						icon:"none"
					})
					reject(err)
				}
			});
		})
	}
	
	get(url,data){
		return this.setOption({
			url,
			data
		})
	}
	
	post(url,data){
		return this.setOption({
			type:"POST",
			url,
			data
		})
	} 
}

export default new Request()